{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Examples and Exercises from Think Stats, 2nd Edition\n",
    "\n",
    "http://thinkstats2.com\n",
    "\n",
    "Copyright 2016 Allen B. Downey\n",
    "\n",
    "MIT License: https://opensource.org/licenses/MIT\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import print_function, division\n",
    "\n",
    "%matplotlib inline\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "import brfss\n",
    "\n",
    "import thinkstats2\n",
    "import thinkplot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The estimation game\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Root mean squared error is one of several ways to summarize the average error of an estimation process."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def RMSE(estimates, actual):\n",
    "    \"\"\"Computes the root mean squared error of a sequence of estimates.\n",
    "\n",
    "    estimate: sequence of numbers\n",
    "    actual: actual value\n",
    "\n",
    "    returns: float RMSE\n",
    "    \"\"\"\n",
    "    e2 = [(estimate-actual)**2 for estimate in estimates]\n",
    "    mse = np.mean(e2)\n",
    "    return np.sqrt(mse)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The following function simulates experiments where we try to estimate the mean of a population based on a sample with size `n=7`.  We run `iters=1000` experiments and collect the mean and median of each sample."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Experiment 1\n",
      "rmse xbar 0.3666916796469523\n",
      "rmse median 0.4440447654801178\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "def Estimate1(n=7, iters=1000):\n",
    "    \"\"\"Evaluates RMSE of sample mean and median as estimators.\n",
    "\n",
    "    n: sample size\n",
    "    iters: number of iterations\n",
    "    \"\"\"\n",
    "    mu = 0\n",
    "    sigma = 1\n",
    "\n",
    "    means = []\n",
    "    medians = []\n",
    "    for _ in range(iters):\n",
    "        xs = [random.gauss(mu, sigma) for _ in range(n)]\n",
    "        xbar = np.mean(xs)\n",
    "        median = np.median(xs)\n",
    "        means.append(xbar)\n",
    "        medians.append(median)\n",
    "\n",
    "    print('Experiment 1')\n",
    "    print('rmse xbar', RMSE(means, mu))\n",
    "    print('rmse median', RMSE(medians, mu))\n",
    "    \n",
    "Estimate1()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using $\\bar{x}$ to estimate the mean works a little better than using the median; in the long run, it minimizes RMSE.  But using the median is more robust in the presence of outliers or large errors.\n",
    "\n",
    "\n",
    "## Estimating variance\n",
    "\n",
    "The obvious way to estimate the variance of a population is to compute the variance of the sample, $S^2$, but that turns out to be a biased estimator; that is, in the long run, the average error doesn't converge to 0.\n",
    "\n",
    "The following function computes the mean error for a collection of estimates."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def MeanError(estimates, actual):\n",
    "    \"\"\"Computes the mean error of a sequence of estimates.\n",
    "\n",
    "    estimate: sequence of numbers\n",
    "    actual: actual value\n",
    "\n",
    "    returns: float mean error\n",
    "    \"\"\"\n",
    "    errors = [estimate-actual for estimate in estimates]\n",
    "    return np.mean(errors)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The following function simulates experiments where we try to estimate the variance of a population based on a sample with size `n=7`.  We run `iters=1000` experiments and two estimates for each sample, $S^2$ and $S_{n-1}^2$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mean error biased -0.11357007322862302\n",
      "mean error unbiased 0.03416824789993983\n"
     ]
    }
   ],
   "source": [
    "def Estimate2(n=7, iters=1000):\n",
    "    mu = 0\n",
    "    sigma = 1\n",
    "\n",
    "    estimates1 = []\n",
    "    estimates2 = []\n",
    "    for _ in range(iters):\n",
    "        xs = [random.gauss(mu, sigma) for i in range(n)]\n",
    "        biased = np.var(xs)\n",
    "        unbiased = np.var(xs, ddof=1)\n",
    "        estimates1.append(biased)\n",
    "        estimates2.append(unbiased)\n",
    "\n",
    "    print('mean error biased', MeanError(estimates1, sigma**2))\n",
    "    print('mean error unbiased', MeanError(estimates2, sigma**2))\n",
    "    \n",
    "Estimate2()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The mean error for $S^2$ is non-zero, which suggests that it is biased.  The mean error for $S_{n-1}^2$ is close to zero, and gets even smaller if we increase `iters`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The sampling distribution\n",
    "\n",
    "The following function simulates experiments where we estimate the mean of a population using $\\bar{x}$, and returns a list of estimates, one from each experiment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def SimulateSample(mu=90, sigma=7.5, n=9, iters=1000):\n",
    "    xbars = []\n",
    "    for j in range(iters):\n",
    "        xs = np.random.normal(mu, sigma, n)\n",
    "        xbar = np.mean(xs)\n",
    "        xbars.append(xbar)\n",
    "    return xbars\n",
    "\n",
    "xbars = SimulateSample()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here's the \"sampling distribution of the mean\" which shows how much we should expect $\\bar{x}$ to vary from one experiment to the next."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAEKCAYAAAD9xUlFAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUe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tyDaQrMbJSZzbDEvPpjN841H7zTv8PMXjWz983sQDMySCsFAAACh0lEQVR7yhJz2t8QjOlhifsXPBLsgUwOJlcLMbNjzewNM3vbzP5kZu0yjKn3+YJxQ8zs38Hezv1m1jzov88SEwXONrNrk8YvMLNrg+d+z8y+28jbVpoYFQUpBs8D3czsIzO718yOTFp2j7sf6O57Aa1J7FFstMET8+L8gcTUABeSmIf/nGCeJoDvAGPdvQ+wCrgg+YmDvZJfA0d7YgK1crLPj1Xn85nZHsDpJCZj2xeoBs4M1r3aExMF9gGONLM+SY+7PHju+4DL699cItmpKEjBc/evgQOAEcAy4AkzOydY/H0ze9PM3iNxH4c9k1bdOOfOe8BsT0wsuJ7EzXM2TkS20N1fD35+DDg85en7Ar2B1y1xF6+hQPcsUet7vn7Bv2NG8Fj9SEy5AHCamb0N/Cf4N/ROetyngu9vAT2yPLdIg+gzBSkK7l5NYm6lV4ICMNTMJpGY7rzM3Rea2WigVdJq64PvNUk/b2xv/N1InQcm09TFL7j74AbErO/5DHjE3a8KPYFZTxJ7AAe6+wozG5/l31GNfqdlC2lPQQqemX3HzHolde0LfMK3fziXB8f5B27Gw+8cfJANiTn4/y9l+XTgMDPbLcjSxsx234zngcRkaAOTZvfc1sy6A1sBa4CVZrY9iTn+RSKhdxVSDNoBv7fEjeyrSMwmOsLdvzKzB0gcrllAYtrmTfU+ib2O+0nMYnlf8kJ3XxYcqppo397x7tckZgHdJO4+x8x+TeKOX81IzKR5obtPN7P/kJixcx7wel2PI7IlNEuqSBZm1oPE/R/2ijmKSM7o8JGIiNTSnoKIiNTSnoKIiNRSURARkVoqCiIiUktFQUREaqkoiIhILRUFERGp9f8BQvXyf8SXzfcAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "cdf = thinkstats2.Cdf(xbars)\n",
    "thinkplot.Cdf(cdf)\n",
    "thinkplot.Config(xlabel='Sample mean',\n",
    "                 ylabel='CDF')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The mean of the sample means is close to the actual value of $\\mu$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "89.9861531227758"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(xbars)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "An interval that contains 90% of the values in the sampling disrtribution is called a 90% confidence interval."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(85.67721360886706, 94.03550577193234)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ci = cdf.Percentile(5), cdf.Percentile(95)\n",
    "ci"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And the RMSE of the sample means is called the standard error."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.4985255047938177"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stderr = RMSE(xbars, 90)\n",
    "stderr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Confidence intervals and standard errors quantify the variability in the estimate due to random sampling."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Estimating rates\n",
    "\n",
    "The following function simulates experiments where we try to estimate the mean of an exponential distribution using the mean and median of a sample. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "rmse L 1.0991868744984798\n",
      "rmse Lm 1.4874648694733599\n",
      "mean error L 0.32089631663544593\n",
      "mean error Lm 0.35349315821339045\n"
     ]
    }
   ],
   "source": [
    "def Estimate3(n=7, iters=1000):\n",
    "    lam = 2\n",
    "\n",
    "    means = []\n",
    "    medians = []\n",
    "    for _ in range(iters):\n",
    "        xs = np.random.exponential(1.0/lam, n)\n",
    "        L = 1 / np.mean(xs)\n",
    "        Lm = np.log(2) / thinkstats2.Median(xs)\n",
    "        means.append(L)\n",
    "        medians.append(Lm)\n",
    "\n",
    "    print('rmse L', RMSE(means, lam))\n",
    "    print('rmse Lm', RMSE(medians, lam))\n",
    "    print('mean error L', MeanError(means, lam))\n",
    "    print('mean error Lm', MeanError(medians, lam))\n",
    "    \n",
    "Estimate3()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The RMSE is smaller for the sample mean than for the sample median.\n",
    "\n",
    "But neither estimator is unbiased."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercises"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exercise:**  In this chapter we used $\\bar{x}$ and median to estimate µ, and found that $\\bar{x}$ yields lower MSE. Also, we used $S^2$ and $S_{n-1}^2$ to estimate σ, and found that $S^2$ is biased and $S_{n-1}^2$ unbiased.\n",
    "Run similar experiments to see if $\\bar{x}$ and median are biased estimates of µ. Also check whether $S^2$ or $S_{n-1}^2$ yields a lower MSE."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Experiment 1\n",
      "mean error xbar 0.0005094416922616375\n",
      "mean error median 0.0008580279056516486\n"
     ]
    }
   ],
   "source": [
    "# Solution\n",
    "\n",
    "def Estimate4(n=7, iters=100000):\n",
    "    \"\"\"Mean error for xbar and median as estimators of population mean.\n",
    "\n",
    "    n: sample size\n",
    "    iters: number of iterations\n",
    "    \"\"\"\n",
    "    mu = 0\n",
    "    sigma = 1\n",
    "\n",
    "    means = []\n",
    "    medians = []\n",
    "    for _ in range(iters):\n",
    "        xs = [random.gauss(mu, sigma) for i in range(n)]\n",
    "        xbar = np.mean(xs)\n",
    "        median = np.median(xs)\n",
    "        means.append(xbar)\n",
    "        medians.append(median)\n",
    "\n",
    "    print('Experiment 1')\n",
    "    print('mean error xbar', MeanError(means, mu))\n",
    "    print('mean error median', MeanError(medians, mu))\n",
    "    \n",
    "Estimate4()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Experiment 2\n",
      "RMSE biased 0.5135938607123939\n",
      "RMSE unbiased 0.5763054869567986\n"
     ]
    }
   ],
   "source": [
    "# Solution\n",
    "\n",
    "def Estimate5(n=7, iters=100000):\n",
    "    \"\"\"RMSE for biased and unbiased estimators of population variance.\n",
    "\n",
    "    n: sample size\n",
    "    iters: number of iterations\n",
    "    \"\"\"\n",
    "    mu = 0\n",
    "    sigma = 1\n",
    "\n",
    "    estimates1 = []\n",
    "    estimates2 = []\n",
    "    for _ in range(iters):\n",
    "        xs = [random.gauss(mu, sigma) for i in range(n)]\n",
    "        biased = np.var(xs)\n",
    "        unbiased = np.var(xs, ddof=1)\n",
    "        estimates1.append(biased)\n",
    "        estimates2.append(unbiased)\n",
    "\n",
    "    print('Experiment 2')\n",
    "    print('RMSE biased', RMSE(estimates1, sigma**2))\n",
    "    print('RMSE unbiased', RMSE(estimates2, sigma**2))\n",
    "\n",
    "Estimate5()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Solution\n",
    "\n",
    "# My conclusions:\n",
    "\n",
    "# 1) xbar and median yield lower mean error as m increases, so neither\n",
    "# one is obviously biased, as far as we can tell from the experiment.\n",
    "\n",
    "# 2) The biased estimator of variance yields lower RMSE than the unbiased\n",
    "# estimator, by about 10%.  And the difference holds up as m increases."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exercise:** Suppose you draw a sample with size n=10 from an exponential distribution with λ=2. Simulate this experiment 1000 times and plot the sampling distribution of the estimate L. Compute the standard error of the estimate and the 90% confidence interval.\n",
    "\n",
    "Repeat the experiment with a few different values of `n` and make a plot of standard error versus `n`.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "standard error 0.825791904537049\n",
      "confidence interval (1.311259184670713, 3.7054533946140884)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.825791904537049"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAEWCAYAAACJ0YulAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzt3XmcVXX9x/HXm2EQREAR3FhVcCHDbcJcSvsVpeaSSalppoloZblkLi2m9ktbzOxXWprmloGGP/uhYmqmlaYGkhuYRog6IJtssi/z+f1xL+OcO/swZ869c9/Px2Me3u/3fufc952R+dzzPed8jyICMzMzgC5ZBzAzs+LhomBmZrVcFMzMrJaLgpmZ1XJRMDOzWi4KZmZWy0XBOiVJl0v6bf7xYEkrJFV0wOueJunJOu0VknZpp21/U9LN+cdDJYWkru207Q77GVlxc1GwdiXpEEl/l7RM0mJJT0n6QJaZIuLNiNgqIjZm8NpbRcSspsZIOkxSdQu2dVVEjG2PXJJmS/pYnW1n9jOy4tIunzLMACT1Bh4AvgTcA3QDPgSszTJXZyCpa0RsyDqHdX7eU7D2tBtARIyPiI0RsToiHomIFwEk7Srpz5LekbRI0l2Stt70zflPr9+Q9KKklZJukbS9pIckvSvpT5K2yY/dNH0yTtJcSW9L+npDoQqnW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XBzMxquSiYmVktFwUzM6vlomBmZrVcFMyaIek0STvVad8saUQ7bHeopM9t7nbM2pOLglnzTgNqi0JEjG2n1WeHAi4KVlRcFKxsSTolf/+G5yXdmF+M7zZJL+fvKXC+pDHklsG+Kz+uh6QnJFXlt7FC0g/zC8j9SdKo/POzJB2THzNU0t8kTct/HZSP8APgQ/ntnp9//R9LmiLpRUmZXLxk5c0Xr1lZkrQn8CPg0xGxXtINwHzgkIgYnR+zdUQslfQEcGFETM3317YlBXBkRDwk6T6gJ7mls0cAt0fEPvkrVGsiYo2k4cD4iKiSdFh+O0fltzsO2C4i/lvSFsBTwGci4vWO+rmYeZkLK1cfBfYHpuSXdegB/BHYRdLPgQeBR1qwnXX57wN4CVibLzIvkZsegtx9IX4haR9gI7BbI9v6ODAyv3cC0AcYDrgoWIdxUbByJXKf5C9NdErfAj4BfAX4LPDFZrazPt7b3a4B1gJERI2kTf++zie3F7I3uSnbNU1k+mpEPNzK92LWbnxMwcrVY8AYSdsBSOoraQjQJSLuBb7De8suvwv02ozX6gO8HRE15BYtrGhkuw8DX8ovgY6k3Ur8JjFWgrynYGUpImZI+ja5u4x1AdYDF5BbJXTTh6VNexG3Ab+StBpoy43gbwDulfQZ4HFyN38BeBHYIOmF/Gv8jNyU07T8UugLgU+14fXM2swHms3MrJanj8zMrJaLgpmZ1XJRMDOzWi4KZmZWy0XBzMxquSiYmVktFwUzM6v1/4PlTLsEFAc8AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Solution\n",
    "\n",
    "def SimulateSample(lam=2, n=10, iters=1000):\n",
    "    \"\"\"Sampling distribution of L as an estimator of exponential parameter.\n",
    "\n",
    "    lam: parameter of an exponential distribution\n",
    "    n: sample size\n",
    "    iters: number of iterations\n",
    "    \"\"\"\n",
    "    def VertLine(x, y=1):\n",
    "        thinkplot.Plot([x, x], [0, y], color='0.8', linewidth=3)\n",
    "\n",
    "    estimates = []\n",
    "    for _ in range(iters):\n",
    "        xs = np.random.exponential(1.0/lam, n)\n",
    "        lamhat = 1.0 / np.mean(xs)\n",
    "        estimates.append(lamhat)\n",
    "\n",
    "    stderr = RMSE(estimates, lam)\n",
    "    print('standard error', stderr)\n",
    "\n",
    "    cdf = thinkstats2.Cdf(estimates)\n",
    "    ci = cdf.Percentile(5), cdf.Percentile(95)\n",
    "    print('confidence interval', ci)\n",
    "    VertLine(ci[0])\n",
    "    VertLine(ci[1])\n",
    "\n",
    "    # plot the CDF\n",
    "    thinkplot.Cdf(cdf)\n",
    "    thinkplot.Config(xlabel='estimate',\n",
    "                     ylabel='CDF',\n",
    "                     title='Sampling distribution')\n",
    "\n",
    "    return stderr\n",
    "\n",
    "SimulateSample()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Solution\n",
    "\n",
    "# My conclusions:\n",
    "\n",
    "# 1) With sample size 10:\n",
    "\n",
    "# standard error 0.762510819389\n",
    "# confidence interval (1.2674054394352277, 3.5377353792673705)\n",
    "\n",
    "# 2) As sample size increases, standard error and the width of\n",
    "#    the CI decrease:\n",
    "\n",
    "# 10      0.90    (1.3, 3.9)\n",
    "# 100     0.21    (1.7, 2.4)\n",
    "# 1000    0.06    (1.9, 2.1)\n",
    "\n",
    "# All three confidence intervals contain the actual value, 2."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exercise:** In games like hockey and soccer, the time between goals is roughly exponential. So you could estimate a team’s goal-scoring rate by observing the number of goals they score in a game. This estimation process is a little different from sampling the time between goals, so let’s see how it works.\n",
    "\n",
    "Write a function that takes a goal-scoring rate, `lam`, in goals per game, and simulates a game by generating the time between goals until the total time exceeds 1 game, then returns the number of goals scored.\n",
    "\n",
    "Write another function that simulates many games, stores the estimates of `lam`, then computes their mean error and RMSE.\n",
    "\n",
    "Is this way of making an estimate biased?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "def SimulateGame(lam):\n",
    "    \"\"\"Simulates a game and returns the estimated goal-scoring rate.\n",
    "\n",
    "    lam: actual goal scoring rate in goals per game\n",
    "    \"\"\"\n",
    "    goals = 0\n",
    "    t = 0\n",
    "    while True:\n",
    "        time_between_goals = random.expovariate(lam)\n",
    "        t += time_between_goals\n",
    "        if t > 1:\n",
    "            break\n",
    "        goals += 1\n",
    "\n",
    "    # estimated goal-scoring rate is the actual number of goals scored\n",
    "    L = goals\n",
    "    return L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Experiment 4\n",
      "rmse L 1.4126517617587144\n",
      "mean error L -0.001049\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYsAAAEKCAYAAADjDHn2AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAFDNJREFUeJzt3X+wX3V95/Hny7CA1tai3P7YhBioQQV/QL1Gu3RtK6Cx7QK7A0OoOnGXHcaOUdfubosjizWlHardnVahLYyk0BaDFqSbaWMRQXRnFEn40WCglBAUbmFLWljdEQUD7/3je+J+/XJzP/fGe/K9lzwfM3dyzud8zvm+bya5r/s5Pz4nVYUkSTN5zrgLkCQtfIaFJKnJsJAkNRkWkqQmw0KS1GRYSJKaDAtJUpNhIUlqMiwkSU0HjbuA+XL44YfXihUrxl2GJC0qt9566z9V1USr37MmLFasWMHWrVvHXYYkLSpJvj6bfp6GkiQ1GRaSpCbDQpLUZFhIkpoMC0lSk2EhSWoyLCRJTYaFJKnJsJAkNT1rnuBeyNZdsHHej3nReWftt+NLkiMLSVKTYS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TSrDnrrCSpyZGFJKnJsJAkNRkWkqQmw0KS1GRYSJKaDAtJUpNhIUlqMiwkSU3/Dx1Fy7KRu8iXAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Solution\n",
    "\n",
    "# The following function simulates many games, then uses the\n",
    "# number of goals scored as an estimate of the true long-term\n",
    "# goal-scoring rate.\n",
    "\n",
    "def Estimate6(lam=2, m=1000000):\n",
    "\n",
    "    estimates = []\n",
    "    for i in range(m):\n",
    "        L = SimulateGame(lam)\n",
    "        estimates.append(L)\n",
    "\n",
    "    print('Experiment 4')\n",
    "    print('rmse L', RMSE(estimates, lam))\n",
    "    print('mean error L', MeanError(estimates, lam))\n",
    "    \n",
    "    pmf = thinkstats2.Pmf(estimates)\n",
    "    thinkplot.Hist(pmf)\n",
    "    thinkplot.Config(xlabel='Goals scored', ylabel='PMF')\n",
    "    \n",
    "Estimate6()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Solution\n",
    "\n",
    "# My conclusions:\n",
    "\n",
    "# 1) RMSE for this way of estimating lambda is 1.4\n",
    "\n",
    "# 2) The mean error is small and decreases with m, so this estimator\n",
    "#    appears to be unbiased.\n",
    "\n",
    "# One note: If the time between goals is exponential, the distribution\n",
    "# of goals scored in a game is Poisson.\n",
    "\n",
    "# See https://en.wikipedia.org/wiki/Poisson_distribution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
